﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Net;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net.Http.Headers;
using Water.Model;
using WaterApp.Common;

namespace WaterApp.Account
{
    public partial class UserForMenu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindUsers();
                BindMenu();
            }
        }

        private void BindUsers()
        {
            AllUser.DataSource = Membership.GetAllUsers();
            AllUser.DataBind();
        }

        private void BindMenu()
        {
            string url = WebapiUrl.weburl() + "GetAllMenus/1/20";
            var httpClient = new HttpClient();
            var responseJson = httpClient.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
            IList<MenuTree> mr = JsonConvert.DeserializeObject<IList<MenuTree>>(responseJson);
            AllMenu.DataSource = mr;
            AllMenu.DataBind();
        }

        protected void cbSel_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (AllUser.SelectedRow == null)
                {
                    return;
                }
                string username = AllUser.SelectedRow.Cells[0].Text;
                string nodeid = ((CheckBox)sender).ToolTip;
                string otype = "1";
                if (((CheckBox)sender).Checked)
                {
                    otype = "0";
                }
                string url = WebapiUrl.weburl() + "AddUserAndNodeid";
                UserAndMenu um = new UserAndMenu(){ UserName = username,SID = nodeid,Otype = otype };
                var requestJson = JsonConvert.SerializeObject(um);
                HttpContent httpContent = new StringContent(requestJson);
                httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                var httpClient = new HttpClient();
                var responseJson = httpClient.PostAsync(url, httpContent).Result.Content.ReadAsStringAsync().Result;                                
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        protected void AllUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindMenu();
        }

        protected void AllMenu_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (AllUser.SelectedRow == null)
            {
                return;
            }
            string uname = AllUser.SelectedRow.Cells[0].Text;
            //(e.Row.RowType == DataControlRowType..AlternatingItem) | |
            //    (e.Row.RowType == DataControlRowType. .SelectedItem)
            if ((e.Row.RowType == DataControlRowType.DataRow))
            {
                IList<string> usernames = getnodeidforuser(uname);//new List<string>() { "4","411" };
                foreach (string li in usernames)
                {
                    if (e.Row.Cells[0].Text.Trim() == li.Trim())
                    {
                        ((CheckBox)e.Row.FindControl("cbSel")).Checked = true;
                    }
                }
            }
        }

        private IList<string> getnodeidforuser(string user)
        {
            string url = WebapiUrl.weburl()+ "GetMenusForuser/" + user;
            var requestJson = JsonConvert.SerializeObject(new MenuTree());
            HttpContent httpContent = new StringContent(requestJson);
            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            var httpClient = new HttpClient();
            var responseJson = httpClient.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
            IList<MenuTree> menulist = JsonConvert.DeserializeObject<IList<MenuTree>>(responseJson);
            IList<string> liststr = new List<string>();
            if (menulist != null)
            {
                foreach (MenuTree mt in menulist)
                {
                    liststr.Add(mt.NodeID);
                }
            }
            return liststr;
        }
        
        protected void AllMenu_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            AllMenu.PageIndex = e.NewPageIndex;
            BindMenu();
        }

    }
}